// pages/login/index.js
import { request } from '../../api/request.js'
const app = getApp();

Page({
  data: {
    phone: "", // 手机号
    phoneCode: "", // 手机验证码
    type: 0,
    active: 1,
    checked: true, //复选框

    send: app.globalData.send,
    // 当前倒计时秒数
    seconds: app.globalData.seconds,
    // 总秒数
    max_seconds: app.globalData.max_seconds,
    // 手机号
    phone: "",
    // 获取倒计时秒数定时器
    time: '',
  },
  onLoad: function (options) {
    var that = this;
    // 如果当前倒计时是停止状态
    if (!app.globalData.seconds) {
      // 开始倒计时
      app.dongtai();
      // 存储显示的电话号
      app.globalData.register_phone = options.phone;
    }
    // 显示验证手机号
    this.setData({
      phone: app.globalData.register_phone,
    })

    console.log(options)


  },
  /**
   * 点击发送动态码按钮
   */
  dongtai: function () {
    console.log(app)
    // 调用倒计时
    app.dongtai();
    // 获取倒计时秒数
    this.listen();
  },
  // 监听全局倒计时秒数
  listen: function () {
    var that = this;
    // 定时查询倒计时数据
    var time = setInterval(function () {
      that.setData({
        // 发送按钮显示
        send: app.globalData.send,
        // 当前倒计时秒数
        seconds: app.globalData.seconds,
        // 总秒数
        max_seconds: app.globalData.max_seconds,
      })
      // 倒计时结束停止定时获取
      if (app.globalData.send) {
        clearInterval(time);
      }
      console.log('time')
    }, 100)
    // 存储定时器,便于清除
    that.setData({
      time: time,
    })
  },
  submit(e) {
    request({
      url: '/user/login',
      method: "POST",
      data: {
        phone: this.data.phone,
        phoneCode: this.data.phoneCode,
        type: 0,
      }
    })
      .then(res => {
        console.log(res)
        const { token } = res.data
        console.log(token)
        wx.setStorageSync('token', token)
        wx.switchTab({
          url: "/pages/workbench/index",
        })
      })
  },
  binBlurIpt(e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  binBlurIptYanZheng(e) {
    this.setData({
      phoneCode: e.detail.value,
    })
  },
  yanzheng(e) {
    console.log(e)
    var that = this
    wx.request({
      url: "http://o2o.kgyj1688.com/O2O_APP/user/login/code",
      method: "POST",
      data: { phone: that.data.phone },
    })
  },
  onChangechecked() {
    this.setData({
      checked: (!event.detail),
    })
  },
  agreement() {
    wx.showToast({
      icon: 'success',
      duration: 1000,
      success: () => {
        wx.navigateTo({
          url: "./compents/agreement/index",
        })
      }
    })
  },







  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.listen();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 暂停获取倒计时数据
    clearInterval(this.data.time);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 暂停获取倒计时数据
    clearInterval(this.data.time);
  },
})